import { GameEventCenter } from "../../GameEventCenter";
import { GlobalObj } from "../../obj/GlobalObj";
import { PGCServer } from "../../PGCServer";
import { DressUpData } from "./DressUpData";
import { DressUpServerDataMgr, DressUpServerTag } from "./DressUpServerDataMgr";

export class LotteryDataMgr {

    private suitList: any;
    private needRefreshList: boolean = true;

    public getListSuitLuckyBox(callback: Laya.Handler) {

        if (!this.needRefreshList && this.suitList)
            return callback && callback.runWith([this.suitList]);

        let type = GlobalObj.isFa()?2:1;
        PGCServer.ins.listSuitLuckyBox(type,Laya.Handler.create(this, (data: any) => {
            if (data && !data.ret) {
                this.suitList = data.data.list;
                this.needRefreshList = false;
                callback && callback.runWith([data.data.list]);
            } else {
                callback && callback.runWith([[]]);
            }
        }))
    }

    public getListGoodsInSuitLuckyBox(id: number, callback: Laya.Handler) {
        PGCServer.ins.listGoodsInSuitLuckyBox(id, Laya.Handler.create(this, (data: any) => {
            if (data && !data.ret) {
                let array = data.data.list;
                let findIdx = -1;
                for (let index = 0; index < array.length; index++) {
                    const goodsData = array[index];
                    if(DressUpServerDataMgr.Ins.containTagByKey("脸部_"+DressUpServerTag.AVATAR_BONE_TAG, goodsData.system_tag_ids)){
                        findIdx = index;
                        break;
                    }
                    
                }
                if(findIdx >= 0){ // 将脸部数据放到数据开始的位置
                    let temp = array[0];
                    array[0] = array[findIdx];
                    array[findIdx] = temp;
                }
                
                callback && callback.runWith([array]);
            } else {
                callback && callback.runWith([[]]);
            }
        }));
    }

    public buyAndOpenSuitLuckyBox(id: number, callback: Laya.Handler) {
        PGCServer.ins.buyAndOpenSuitLuckyBox(id, Laya.Handler.create(this, (data: any) => {
            if (data && !data.ret) {
                this.needRefreshList = true;
                callback && callback.runWith([data.data.reward]);
                GameEventCenter.Ins.event(GameEventCenter.UPDATE_CRYSTAL);
            }
        }));
    }

    /** 获取套转内数组 */
    public getLuckyBoxDressList(ids:number[],istest:boolean):DressUpData[]{
        if(istest){

            let datas = DressUpServerDataMgr.Ins.allGoodsMap.get(DressUpServerTag.ZHUANG_RONG_TAG);
            return datas.filter(value=>value.preform_attribute_desc.length > 0).slice(0, 6);
        }
        if(ids){
            let dressUpList:any[] = [], data:any;
            for (let index = 0; index < ids.length; index++) {
                const id = ids[index];
                
                data = DressUpServerDataMgr.Ins.getDressUpDataById(id);
                data && dressUpList.push(data);
            }
            return dressUpList;
        }
    }
}